/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas;

import controladores.RepuestoJpaController;
import entidades.Categoria;
import entidades.CategoriaRepuesto;
import entidades.Marca;
import entidades.MarcaRepuesto;
import entidades.ModeloRepuesto;
import entidades.ProveedorRepuesto;
import entidades.Repuesto;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import reportes.GenReportes;

import rn.RepuestoRN;

/**
 *
 * @author Pablo
 */
public class Repuestos extends javax.swing.JFrame {

    private Repuesto repuesto, repuesto_seleccionado;
    DefaultTableModel modelo_de_tabla;
    private int opcion_modificar = 0;
    private int ancho;

    public Repuestos() {
        initComponents();
        llenarCombos();
        repuesto = new Repuesto();

        bt_modificar.setEnabled(false);
        bt_eliminar.setEnabled(false);

        tf_descripcion.setDocument(new JTextFieldToUpperCase());
        tf_nro_parte.setDocument(new JTextFieldToUpperCase());

        tf_descripcion.getDocument().putProperty("campoDelEvento", "descripcion");
        tf_descripcion.getDocument().addDocumentListener(new TexChangeListener());
        tf_descripcion.addKeyListener(new KeyPressedClass());
        tf_nro_parte.getDocument().putProperty("campoDelEvento", "nro_parte");
        tf_nro_parte.getDocument().addDocumentListener(new TexChangeListener());
        tf_nro_parte.addKeyListener(new KeyPressedClass());

        tf_nro_parte.addKeyListener(new CambiarCampo());
        tf_descripcion.addKeyListener(new CambiarCampo());
        tf_codigo_barras.addKeyListener(new CambiarCampo());
        combo_marcas.addKeyListener(new CambiarCampo());
        combo_modelos.addKeyListener(new CambiarCampo());
        combo_categorias.addKeyListener(new CambiarCampo());
        bt_grabar.addKeyListener(new GrabarRegistro());

//        tabla_conincidencias.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "Enter");
//        tabla_conincidencias.getActionMap().put("Enter", new AbstractAction() {
//            @Override
//            public void actionPerformed(ActionEvent ae) {
//                System.out.println("Se presiono la tecla enter");
//                int fila = tabla_conincidencias.getSelectedRow();
//                String s_id = (String) tabla_conincidencias.getValueAt(fila, 0);
//                repuesto_seleccionado = RepuestoRN.findRepuestoPorID(new Long(s_id));
//                llenarCampos(repuesto_seleccionado);
//                habilitarCampos(false);
//                bt_grabar.setEnabled(false);
//                bt_modificar_stock.setEnabled(false);
//                bt_modificar.setEnabled(true);
//                //bt_eliminar.setEnabled(true);
//                //tabla_conincidencias.requestFocus();
//                //tabla_conincidencias.changeSelection(fila, 0, false, false);
//            }
//        });

        tabla_conincidencias.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getSource() == tabla_conincidencias) {
                    if (e.VK_ENTER == e.getKeyCode()) {                        
                        int fila = tabla_conincidencias.getSelectedRow();
                        String s_id = (String) tabla_conincidencias.getValueAt(fila, 0);
                        repuesto_seleccionado = RepuestoRN.findRepuestoPorID(new Long(s_id));
                        llenarCampos(repuesto_seleccionado);
                        habilitarCampos(false);
                        bt_grabar.setEnabled(false);
                        bt_modificar_stock.setEnabled(false);
                        bt_modificar.setEnabled(true);
                    }
                }
            }
        });

        //Para accionar con doble clic
        tabla_conincidencias.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent me) {
                JTable table = (JTable) me.getSource();
                Point p = me.getPoint();
                int row = table.rowAtPoint(p);
                if (me.getClickCount() == 2) {
                    String s_id = (String) tabla_conincidencias.getValueAt(row, 0);
                    repuesto_seleccionado = RepuestoRN.findRepuestoPorID(new Long(s_id));
                    llenarCampos(repuesto_seleccionado);
                    habilitarCampos(false);
                    bt_grabar.setEnabled(false);
                    bt_modificar_stock.setEnabled(false);
                    bt_modificar.setEnabled(true);                   
                }
            }
        });

        this.setLocationRelativeTo(null);
        //tf_nro_parte.requestFocus();
        ancho = tabla_conincidencias.getWidth();
        iniciarModeloTabla();
        iniciarModeloColumna();
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    class CambiarCampo extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            accion(e);
        }

        public void accion(KeyEvent e) {
            if (e.getSource() == tf_nro_parte) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    tf_codigo_barras.requestFocus();
                }
            }
            if (e.getSource() == tf_codigo_barras) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    combo_marcas.requestFocus();
                }
            }
            if (e.getSource() == combo_marcas) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    combo_modelos.requestFocus();
                }
            }
            if (e.getSource() == combo_modelos) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    combo_categorias.requestFocus();
                }
            }
            if (e.getSource() == combo_categorias) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    tf_descripcion.requestFocus();
                }
            }
            if (e.getSource() == tf_descripcion) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    tf_precio_unitario.requestFocus();
                }
            }
            if (e.getSource() == tf_precio_unitario) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    bt_grabar.requestFocus();
                }
            }
        }

    }

    @SuppressWarnings("unchecked")

    public boolean verificarDatosRepuesto(Repuesto v, Long id_excepcion) {
        boolean b = true;
//        if (RepuestoRN.yaExiste(v.getDescripcion(), RepuestoJpaController.DESCRIPCION, id_excepcion)) {
//            b = false;
//            mostrarMensaje("Ya existe un repuesto con la misma 'descripción'");
//        }
        if (RepuestoRN.yaExiste(v.getNro_parte(), RepuestoJpaController.NRO_PARTE, id_excepcion)) {
            b = false;
            mostrarMensaje("Ya existe un repuesto con la misma 'nro_parte'");
        }
        return b;
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        tf_combustible_actual1 = new javax.swing.JTextField();
        bt_ingresar1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        tf_combustible_actual4 = new javax.swing.JTextField();
        bt_cancelar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        tf_nro_parte = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        tf_descripcion = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        combo_marcas = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();
        combo_modelos = new javax.swing.JComboBox();
        combo_categorias = new javax.swing.JComboBox();
        bt_marcas = new javax.swing.JButton();
        bt_modelos = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        bt_grabar = new javax.swing.JButton();
        bt_limpiar = new javax.swing.JButton();
        bt_modificar = new javax.swing.JButton();
        jLabel19 = new javax.swing.JLabel();
        tf_duracion_meses = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        tf_precio_unitario = new javax.swing.JTextField();
        bt_eliminar = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        tf_codigo_barras = new javax.swing.JTextField();
        bt_modificar_stock = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        tabla_conincidencias = new javax.swing.JTable();
        bt_imprimir = new javax.swing.JButton();
        bt_cancelar3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        combo_listados = new javax.swing.JComboBox();
        tx_categoria = new javax.swing.JLabel();
        combo_categoria_filtro = new javax.swing.JComboBox();
        tx_marca = new javax.swing.JLabel();
        combo_marca_filtro = new javax.swing.JComboBox();

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("Combustible Actual (M3):");

        tf_combustible_actual1.setEnabled(false);

        bt_ingresar1.setText("INGRESAR");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel5.setText("Ingreso (M3):");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Ingreso (M3):");

        tf_combustible_actual4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        bt_cancelar.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        bt_cancelar.setText("CANCELAR");
        bt_cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelarActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(0, 0, 153));

        jLabel2.setBackground(new java.awt.Color(255, 255, 255));
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("ARCHIVO DE REPUESTOS Y PARTES");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        tf_nro_parte.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Nº de parte:");

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel13.setText("Descripción:");

        tf_descripcion.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        tf_descripcion.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                tf_descripcionFocusGained(evt);
            }
        });

        jLabel14.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel14.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel14.setText(" Marca:");

        jLabel15.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel15.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel15.setText("Categoria:");

        combo_marcas.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_marcas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_marcasActionPerformed(evt);
            }
        });

        jLabel16.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel16.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel16.setText("Modelo:");

        combo_modelos.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        combo_categorias.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_categorias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_categoriasActionPerformed(evt);
            }
        });

        bt_marcas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_redo.png"))); // NOI18N
        bt_marcas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_marcasActionPerformed(evt);
            }
        });

        bt_modelos.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_redo.png"))); // NOI18N
        bt_modelos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_modelosActionPerformed(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_redo.png"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        bt_grabar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_grabar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept.png"))); // NOI18N
        bt_grabar.setText("GRABAR");
        bt_grabar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_grabarActionPerformed(evt);
            }
        });

        bt_limpiar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_limpiar.setText("LIMPIAR");
        bt_limpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_limpiarActionPerformed(evt);
            }
        });

        bt_modificar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_modificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/pencil.png"))); // NOI18N
        bt_modificar.setText("MODIFICAR");
        bt_modificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_modificarActionPerformed(evt);
            }
        });

        jLabel19.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel19.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel19.setText("Duración Apox. (Meses):");
        jLabel19.setEnabled(false);

        tf_duracion_meses.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        tf_duracion_meses.setEnabled(false);
        tf_duracion_meses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_duracion_mesesActionPerformed(evt);
            }
        });

        jLabel22.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel22.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel22.setText("Precio Unitario:");

        tf_precio_unitario.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        bt_eliminar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        bt_eliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        bt_eliminar.setText("ELIMIMAR");
        bt_eliminar.setEnabled(false);
        bt_eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_eliminarActionPerformed(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Codigo de barras:");

        tf_codigo_barras.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        bt_modificar_stock.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/application_edit.png"))); // NOI18N
        bt_modificar_stock.setText("MODIFICAR STOCK");
        bt_modificar_stock.setEnabled(false);
        bt_modificar_stock.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_modificar_stockActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap(13, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel19)
                                .addGap(10, 10, 10)
                                .addComponent(tf_duracion_meses, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(bt_eliminar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bt_modificar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bt_limpiar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(bt_grabar))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel22)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(bt_modificar_stock, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(tf_precio_unitario, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)))))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(108, 108, 108)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel15)
                                    .addComponent(jLabel14)
                                    .addComponent(jLabel16)))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(94, 94, 94)
                                .addComponent(jLabel8))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel13, javax.swing.GroupLayout.Alignment.TRAILING))))
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tf_descripcion)
                            .addComponent(tf_codigo_barras)
                            .addComponent(tf_nro_parte, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(combo_marcas, 0, 223, Short.MAX_VALUE)
                                    .addComponent(combo_modelos, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(combo_categorias, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(bt_marcas, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(bt_modelos, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_nro_parte, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_codigo_barras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel14)
                        .addComponent(combo_marcas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bt_marcas, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel16)
                        .addComponent(combo_modelos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(bt_modelos, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel15)
                        .addComponent(combo_categorias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_descripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_duracion_meses, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_precio_unitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel22))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bt_modificar_stock)
                .addGap(31, 31, 31)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bt_grabar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_limpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_modificar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bt_eliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        tabla_conincidencias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Documento", "Nombre"
            }
        ));
        jScrollPane4.setViewportView(tabla_conincidencias);

        bt_imprimir.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_imprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/printer.png"))); // NOI18N
        bt_imprimir.setText("IMPRIMIR LISTA");
        bt_imprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_imprimirActionPerformed(evt);
            }
        });

        bt_cancelar3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        bt_cancelar3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_excel.png"))); // NOI18N
        bt_cancelar3.setText("EXPORTAR");
        bt_cancelar3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bt_cancelar3ActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Listar"));

        combo_listados.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        combo_listados.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Seleccionar...", "Todos", "Marca:", "Categoria:" }));
        combo_listados.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_listadosActionPerformed(evt);
            }
        });

        tx_categoria.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_categoria.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tx_categoria.setText("Categ.:");
        tx_categoria.setEnabled(false);

        combo_categoria_filtro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_categoria_filtro.setEnabled(false);
        combo_categoria_filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_categoria_filtroActionPerformed(evt);
            }
        });

        tx_marca.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        tx_marca.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        tx_marca.setText("Marca:");
        tx_marca.setEnabled(false);

        combo_marca_filtro.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        combo_marca_filtro.setEnabled(false);
        combo_marca_filtro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_marca_filtroActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addComponent(combo_listados, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38)
                .addComponent(tx_marca)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(combo_marca_filtro, 0, 170, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(tx_categoria)
                .addGap(7, 7, 7)
                .addComponent(combo_categoria_filtro, 0, 170, Short.MAX_VALUE)
                .addGap(82, 82, 82))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(combo_listados, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(combo_marca_filtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tx_marca))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(combo_categoria_filtro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tx_categoria)))
                .addGap(0, 40, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 787, Short.MAX_VALUE)
                            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(719, 719, 719)
                        .addComponent(bt_cancelar3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_imprimir)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bt_cancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(bt_cancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                    .addComponent(bt_imprimir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(bt_cancelar3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void combo_categoria_filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_categoria_filtroActionPerformed
        List<Repuesto> resultado = new ArrayList();
        CategoriaRepuesto item_selected = (CategoriaRepuesto) combo_categoria_filtro.getSelectedItem();
        resultado = RepuestoRN.getRepuestosPor(item_selected.toString(), "Categoria");
        llenarTablaResultado(resultado);
    }//GEN-LAST:event_combo_categoria_filtroActionPerformed

    private void tf_duracion_mesesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_duracion_mesesActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_duracion_mesesActionPerformed

    private void bt_grabarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_grabarActionPerformed
        grabarDatos();
    }//GEN-LAST:event_bt_grabarActionPerformed

    private void grabarDatos() {
        try {
            grabar();
        } catch (Exception ex) {
            mostrarMensaje(ex.getMessage());
        }
    }

    private void grabar() throws Exception {
        if (!chaquearCamposRequeridos()) {
            tf_nro_parte.requestFocus();
            throw new Exception("Campos requeridos no pueden estar vacios!!!");
        }
        if (!chequearTipoDatos()) {
            throw new Exception("Precio Invalido");
        }
        Long id_excep = null;
        if (opcion_modificar == 1) {
            id_excep = repuesto_seleccionado.getId();
        }
        setearRepuesto();
        if (verificarDatosRepuesto(repuesto, id_excep)) {
            if (opcion_modificar == 1) {
                repuesto.setId(repuesto_seleccionado.getId());
                repuesto.setStock(repuesto_seleccionado.getStock());
                RepuestoRN.edit(repuesto);
                mostrarMensaje("Datos Modificados!!!");
                opcion_modificar = 0;
            } else {
                RepuestoRN.create(repuesto);
                mostrarMensaje("Registro Guardado!!!");
                opcion_modificar = 0;
            }
            limpiarCampos();
            repuesto = new Repuesto();
            bt_modificar.setEnabled(false);
            bt_modificar_stock.setEnabled(false);
            tf_nro_parte.requestFocus();
        }
        repuesto_seleccionado = null;
    }

    private void bt_limpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_limpiarActionPerformed
        limpiarCampos();
        iniciarModeloTabla();
        tabla_conincidencias.setModel(modelo_de_tabla);
        habilitarCampos(true);
        bt_grabar.setEnabled(true);
        bt_modificar.setEnabled(false);
        bt_modificar_stock.setEnabled(false);
        bt_eliminar.setEnabled(false);
        opcion_modificar = 0;
        repuesto_seleccionado = null;
        tf_nro_parte.requestFocus();
    }//GEN-LAST:event_bt_limpiarActionPerformed

    private void bt_modificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_modificarActionPerformed
        habilitarCampos(true);
        bt_grabar.setEnabled(true);
        bt_modificar_stock.setEnabled(true);
        opcion_modificar = 1;
    }//GEN-LAST:event_bt_modificarActionPerformed

    private void bt_eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_eliminarActionPerformed
        if (repuesto_seleccionado != null) {
            int c = JOptionPane.showConfirmDialog(null, "¿Está seguro de eliminar el rodado?");
            System.out.println(c);
            if (c == 0) {
                RepuestoRN.eliminar(repuesto_seleccionado.getId());
                mostrarMensaje("Rodado eliminado!!!");
                limpiarCampos();
                habilitarCampos(true);
                bt_grabar.setEnabled(true);
                bt_modificar.setEnabled(false);
                bt_modificar_stock.setEnabled(false);
                bt_eliminar.setEnabled(false);
                repuesto_seleccionado = null;
                tf_nro_parte.requestFocus();
            }
        } else {
            mostrarMensaje("Debe seleccionar un rodado");
        }
    }//GEN-LAST:event_bt_eliminarActionPerformed

    private void combo_marcasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_marcasActionPerformed
        if (combo_marcas.getSelectedItem() != null) {
            MarcaRepuesto item_selected = (MarcaRepuesto) combo_marcas.getSelectedItem();
            List<ModeloRepuesto> modeloRepuestosPorMarcaRepuesto = RepuestoRN.getModeloRepuestosPorMarcaRepuesto(item_selected.getId());
            combo_modelos.removeAllItems();
            for (ModeloRepuesto m : modeloRepuestosPorMarcaRepuesto) {
                combo_modelos.addItem(m);

            }
        }
    }//GEN-LAST:event_combo_marcasActionPerformed

    private void combo_listadosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_listadosActionPerformed
        List<Repuesto> resultado = new ArrayList();
        String item_selected = (String) combo_listados.getSelectedItem();
        if (!item_selected.equals("Seleccionar...")) {
            switch (item_selected) {
                case "Todos":
                    habilitarFiltro(false);
                    resultado = RepuestoRN.getTodosVechiculos();
                    break;
//                case "Proveedor:":
//                    habilitarFiltro(false);
//                    tx_proveedor.setEnabled(true);
//                    combo_proveedor_filtro.setEnabled(true);
//                    break;
                case "Marca:":
                    habilitarFiltro(false);
                    tx_marca.setEnabled(true);
                    combo_marca_filtro.setEnabled(true);
                    break;
                case "Categoria:":
                    habilitarFiltro(false);
                    tx_categoria.setEnabled(true);
                    combo_categoria_filtro.setEnabled(true);
                    break;
            }
            llenarTablaResultado(resultado);
        }
    }//GEN-LAST:event_combo_listadosActionPerformed

    private void combo_marca_filtroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_marca_filtroActionPerformed
        List<Repuesto> resultado = new ArrayList();
        MarcaRepuesto item_selected = (MarcaRepuesto) combo_marca_filtro.getSelectedItem();
        resultado = RepuestoRN.getRepuestosPor(item_selected.toString(), "Marca");
        llenarTablaResultado(resultado);
    }//GEN-LAST:event_combo_marca_filtroActionPerformed

    private void bt_imprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_imprimirActionPerformed
        String item_selected = (String) combo_listados.getSelectedItem();
        GenReportes gen = new GenReportes();
        if (!item_selected.equals("Seleccionar...")) {
            switch (item_selected) {
                case "Todos":
                    gen.repTodosRepuestos();
                    break;
//                case "Proveedor:":
//                    ProveedorRepuesto p = (ProveedorRepuesto)combo_proveedor_filtro.getSelectedItem();
//                    gen.repRepuestosPorProveedor(p.getDescripcion());
//                    break;
                case "Marca:":
                    MarcaRepuesto m = (MarcaRepuesto) combo_marca_filtro.getSelectedItem();
                    gen.repRepuestosPorMarca(m.getDescripcion());
                    break;
                case "Categoria:":
                    CategoriaRepuesto cat = (CategoriaRepuesto) combo_categoria_filtro.getSelectedItem();
                    gen.repRepuestosPorCategoria(cat.getDescripcion());
                    break;
            }
        }
    }//GEN-LAST:event_bt_imprimirActionPerformed

    private void bt_marcasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_marcasActionPerformed
        new ABM_MarcasRepuesto(this);
    }//GEN-LAST:event_bt_marcasActionPerformed

    private void bt_modelosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_modelosActionPerformed
        new ABM_ModelosRepuesto(this, (MarcaRepuesto) combo_marcas.getSelectedItem());
    }//GEN-LAST:event_bt_modelosActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        new ABM_CategoriasRepuesto(this);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void bt_cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_bt_cancelarActionPerformed

    private void bt_modificar_stockActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_modificar_stockActionPerformed
        new FormIngresoRepuestos(repuesto_seleccionado);
    }//GEN-LAST:event_bt_modificar_stockActionPerformed

    private void combo_categoriasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_categoriasActionPerformed

    }//GEN-LAST:event_combo_categoriasActionPerformed

    private void tf_descripcionFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tf_descripcionFocusGained
        generarDescripcion();
    }//GEN-LAST:event_tf_descripcionFocusGained

    private void bt_cancelar3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bt_cancelar3ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_bt_cancelar3ActionPerformed

    private void generarDescripcion() {
        String nro_parte = tf_nro_parte.getText();
        if (nro_parte != null) {
            if (combo_categorias.getSelectedItem() != null) {
                CategoriaRepuesto cat = (CategoriaRepuesto) combo_categorias.getSelectedItem();
                MarcaRepuesto mar = (MarcaRepuesto) combo_marcas.getSelectedItem();
                System.out.println(mar.getDescripcion());
                tf_descripcion.setText(cat.getDescripcion().concat(" ").concat(mar.getDescripcion()).concat(" - ").concat(nro_parte));
            }
        }
    }

    private void habilitarFiltro(boolean b) {
        tx_categoria.setEnabled(b);
        combo_categoria_filtro.setEnabled(b);
        tx_marca.setEnabled(b);
        combo_marca_filtro.setEnabled(b);
    }

    private void setearRepuesto() {
        //Fields
        repuesto.setDescripcion(tf_descripcion.getText());
        repuesto.setNro_parte(tf_nro_parte.getText());
//        if (!tf_duracion_km.getText().isEmpty()) {
//            repuesto.setDuracion_aprox_km(Integer.parseInt(tf_duracion_km.getText()));
//        }
        if (!tf_duracion_meses.getText().isEmpty()) {
            repuesto.setDuracion_aprox_meses(Integer.parseInt(tf_duracion_meses.getText()));
        }
        if (!tf_precio_unitario.getText().isEmpty()) {
            repuesto.setPrecio_unitario(Double.parseDouble(tf_precio_unitario.getText()));
        }
        repuesto.setCodigo_barras(tf_codigo_barras.getText());
        repuesto.setStock(0);

        //Combos
        repuesto.setCategoriaRepuesto((CategoriaRepuesto) combo_categorias.getSelectedItem());
        repuesto.setModeloRepuesto((ModeloRepuesto) combo_modelos.getSelectedItem());
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bt_cancelar;
    private javax.swing.JButton bt_cancelar3;
    private javax.swing.JButton bt_eliminar;
    private javax.swing.JButton bt_grabar;
    private javax.swing.JButton bt_imprimir;
    private javax.swing.JButton bt_ingresar1;
    private javax.swing.JButton bt_limpiar;
    private javax.swing.JButton bt_marcas;
    private javax.swing.JButton bt_modelos;
    private javax.swing.JButton bt_modificar;
    private javax.swing.JButton bt_modificar_stock;
    private javax.swing.JComboBox combo_categoria_filtro;
    public javax.swing.JComboBox combo_categorias;
    private javax.swing.JComboBox combo_listados;
    private javax.swing.JComboBox combo_marca_filtro;
    public javax.swing.JComboBox combo_marcas;
    public javax.swing.JComboBox combo_modelos;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTable tabla_conincidencias;
    private javax.swing.JTextField tf_codigo_barras;
    private javax.swing.JTextField tf_combustible_actual1;
    private javax.swing.JTextField tf_combustible_actual4;
    private javax.swing.JTextField tf_descripcion;
    private javax.swing.JTextField tf_duracion_meses;
    private javax.swing.JTextField tf_nro_parte;
    private javax.swing.JTextField tf_precio_unitario;
    private javax.swing.JLabel tx_categoria;
    private javax.swing.JLabel tx_marca;
    // End of variables declaration//GEN-END:variables

    private boolean chequearTipoDatos() {
        //Se verifica si el precio es correcto.
        if (!tf_precio_unitario.getText().isEmpty()) {
            try {
                Double.parseDouble(tf_precio_unitario.getText());
                return true;
            } catch (NumberFormatException e) {
                return false;
            }
        }
        return true;
    }

    private boolean chaquearCamposRequeridos() {

        ArrayList<JTextField> campos_requeridos = new ArrayList();
        campos_requeridos.add(tf_descripcion);
        campos_requeridos.add(tf_nro_parte);

        for (JTextField t : campos_requeridos) {
            if (t.getText().startsWith(" ") || t.getText().isEmpty()) {
                return false;
            }
        }

        return true;
    }

    private void limpiarCampos() {
        tf_descripcion.setText(null);
        //tf_duracion_km.setText(null);
        tf_duracion_meses.setText(null);
        tf_precio_unitario.setText(null);
        tf_nro_parte.setText(null);
        tf_codigo_barras.setText(null);
    }

    private void mostrarMensaje(String mensaje) {
        JOptionPane.showMessageDialog(null, mensaje);
    }

    public void llenarCombos() {
        List<CategoriaRepuesto> todosCategoria = RepuestoRN.getTodosCategoriaRepuesto();
        List<MarcaRepuesto> todosMarca = RepuestoRN.getTodosMarcaRepuesto();
        List<ModeloRepuesto> todosModelo = RepuestoRN.getTodosModeloRepuesto();
        List<ProveedorRepuesto> todosProveedorRepuestos = RepuestoRN.getTodosProveedorRepuesto();

        llenarCombo(combo_categorias, todosCategoria);
        llenarCombo(combo_marcas, todosMarca);
        llenarCombo(combo_categoria_filtro, todosCategoria);
        llenarCombo(combo_marca_filtro, todosMarca);
    }

    public void llenarCombo(JComboBox combo, List lista) {
        for (Object c : lista) {
            combo.addItem(c);
        }
    }

    private void llenarTablaResultado(List<Repuesto> resultado) {
        iniciarModeloTabla();
        for (Repuesto c : resultado) {

            Object fila[] = new Object[]{
                c.getId().toString(), c.getDescripcion(), c.getNro_parte(), c.getStock(), c.getPrecio_unitario()
            };
            modelo_de_tabla.addRow(fila);
        }
        tabla_conincidencias.setModel(modelo_de_tabla);
        iniciarModeloColumna();
    }

    public class TexChangeListener implements DocumentListener {

        public void insertUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }

        public void removeUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }

        public void changedUpdate(javax.swing.event.DocumentEvent evt) {
            LlenarTablaConicidencias(evt);
        }
    }

    private void iniciarModeloTabla() {
        Object datos[][] = {};
        String[] columnNames = {"Id", "Descripcion", "Nro Parte", "Stock", "Precio"};
        modelo_de_tabla = new DefaultTableModel(datos, columnNames);
    }

    private void iniciarModeloColumna() {
        tabla_conincidencias.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumnModel columnModel = tabla_conincidencias.getColumnModel();
        columnModel.getColumn(0).setPreferredWidth((5 * ancho) / 100);
        columnModel.getColumn(1).setPreferredWidth((60 * ancho) / 100);
        columnModel.getColumn(2).setPreferredWidth((10 * ancho) / 100);
        columnModel.getColumn(3).setPreferredWidth((10 * ancho) / 100);
        columnModel.getColumn(4).setPreferredWidth((14 * ancho) / 100);
    }

    public void LlenarTablaConicidencias(javax.swing.event.DocumentEvent evt) {

        String campo_del_evento = (String) evt.getDocument().getProperty("campoDelEvento");
        List<Repuesto> listaPersonas = new ArrayList<>();

        if (campo_del_evento.equals("descripcion")) {
            listaPersonas = RepuestoRN.getRepuestosLike(tf_descripcion.getText(), RepuestoJpaController.DESCRIPCION);
        }
        if (campo_del_evento.equals("nro_parte")) {
            listaPersonas = RepuestoRN.getRepuestosLike(tf_nro_parte.getText(), RepuestoJpaController.NRO_PARTE);
        }

        iniciarModeloTabla();

        for (Repuesto c : listaPersonas) {

            Object fila[] = new Object[]{
                c.getId().toString(), c.getDescripcion(), c.getNro_parte(), c.getStock(), c.getPrecio_unitario()
            };
            modelo_de_tabla.addRow(fila);
        }
        tabla_conincidencias.setModel(modelo_de_tabla);
        iniciarModeloColumna();
    }

    public class KeyPressedClass implements KeyListener {

        @Override
        public void keyTyped(KeyEvent e) {
            accion(e);
        }

        @Override
        public void keyPressed(KeyEvent e) {
            accion(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            accion(e);
        }

        public void accion(KeyEvent e) {
            if (e.getSource() == tf_descripcion || e.getSource() == tf_nro_parte) {
                if (e.VK_DOWN == e.getKeyCode()) {
                    tabla_conincidencias.requestFocus();
                    tabla_conincidencias.changeSelection(0, 0, false, false);
                }
            }
        }
    }

    class GrabarRegistro extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getSource() == bt_grabar) {
                if (e.VK_ENTER == e.getKeyCode()) {
                    grabarDatos();
                }
            }
        }
    }

    private void llenarCampos(Repuesto resul) {
        //Fields
        tf_descripcion.setText(resul.getDescripcion());
        //tf_duracion_km.setText("" + resul.getDuracion_aprox_km());
        tf_duracion_meses.setText("" + resul.getDuracion_aprox_meses());
        tf_precio_unitario.setText("" + resul.getPrecio_unitario());
        tf_nro_parte.setText(resul.getNro_parte());
        tf_codigo_barras.setText(resul.getCodigo_barras());

        //Combos
        combo_categorias.setSelectedItem(resul.getCategoriaRepuesto());
        combo_marcas.setSelectedItem(resul.getModeloRepuesto().getMarca());
        combo_modelos.setSelectedItem(resul.getModeloRepuesto());
    }

    private void habilitarCampos(boolean b) {
        tf_descripcion.setEditable(b);
        //tf_duracion_km.setEditable(b);
        tf_duracion_meses.setEditable(b);
        tf_precio_unitario.setEditable(b);
        tf_nro_parte.setEditable(b);
        tf_codigo_barras.setEditable(b);
        //Combos
        combo_categorias.setEnabled(b);
        combo_marcas.setEnabled(b);
        combo_modelos.setEnabled(b);

    }

    class JTextFieldToUpperCase extends PlainDocument {

        public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
            super.insertString(offset, str.toUpperCase(), attr);
        }
    }

}
